package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"shangyuapi/utils"
	"time"
)

type UserlistRBody struct {
	Data       []models.User `json:"data"`
	PageNo     int           `json:"pageNo"`
	PageSize   int           `json:"pageSize"`
	TotalCount int64         `json:"totalCount"`
	TotalPage  int64         `json:"totalPage"`
}

type UserlistBody struct {
	State       int    `json:"state"`
	Page        int    `json:"page"`
	Pagesize    int    `json:"pagesize"`
	Cityid      int64  `json:"cityid" gorm:"cityid"`       // 城市
	Sex         int64  `json:"sex" gorm:"sex"`             // 1男 2女
	Agemin      int64  `json:"agemin" gorm:"agemin"`       //
	Agemax      int64  `json:"agemax" gorm:"agemax"`       //
	Marriage    string `json:"marriage" gorm:"marriage"`   // 婚姻情况
	Income      string `json:"income" gorm:"income"`       // 收入
	Education   string `json:"education" gorm:"education"` // 学历
	Name        string `json:"name" gorm:"name"`
	Hongniangid int64  `json:"hongniangid" gorm:"hongniangid"`
}

func Userlist(ctx *gin.Context) {
	request := UserlistBody{}
	ctx.ShouldBind(&request)
	state := request.State
	hongniangid := request.Hongniangid
	cityid := request.Cityid
	sex := request.Sex
	name := request.Name
	agemin := request.Agemin
	agemax := request.Agemax
	marriage := request.Marriage
	income := request.Income
	education := request.Education
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)

	if hongniangid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "红娘id不能为空",
		})
		return
	} else {
		hnlog := models.Hongnianglog{
			Hongniangid: hongniangid,
			Addtime:     time.Now().Unix(),
			Page:        int64(currentPage),
			Title:       "查看用户信息第" + utils.Int64string(int64(currentPage)) + "页",
		}
		data_source.Db.Create(&hnlog)
	}

	var count int64
	var users []models.User
	res := data_source.Db.Select("id,nickname,city,logo,shengri,name,xingzuo,height,weight,career,company,income,education,province,school,nationality,huji,children,marriage,belief,smoke,wine,biaoqian,title,family,love,images")
	if state == 2 {
		xctime := time.Now().Unix() - 86400*30
		res = res.Where("updatetime < ?", xctime)
	}
	if name != "" {
		res = res.Where("name like ?", "%"+name+"%")
	}
	if cityid > 0 {
		res = res.Where("cityid = ?", cityid)
	}
	if sex > 0 {
		res = res.Where("sex = ?", sex)
	}
	if agemin > 0 {
		res = res.Where("age >= ?", agemin)
	}
	if agemax > 0 {
		res = res.Where("age <= ?", agemax)
	}
	if marriage != "" {
		res = res.Where("marriage = ?", marriage)
	}
	if income != "" {
		res = res.Where("income = ?", income)
	}
	if education != "" {
		res = res.Where("education = ?", education)
	}

	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("updatetime desc").Find(&users)

	var uidarr []int64
	for _, u := range users {
		uidarr = append(uidarr, u.ID)
	}
	var collects []models.Collectuser
	data_source.Db.Where("hongniangid = ?", hongniangid).Where("uid in (?)", uidarr).Find(&collects)

	for k, u := range users {
		users[k].Shoucong = 1
		for _, c := range collects {
			if u.ID == c.Uid {
				users[k].Shoucong = 2
			}
		}
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Danshentuans(ctx *gin.Context) {
	request := UserlistBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	pageSize := request.Pagesize
	currentPage := request.Page
	offsetNum := pageSize * (currentPage - 1)
	if hongniangid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "红娘id不能为空",
		})
		return
	}

	var collects []models.Collectuser
	data_source.Db.Where("hongniangid = ?", hongniangid).Find(&collects)
	var useridarr []int64
	for _, c := range collects {
		useridarr = append(useridarr, c.Uid)
	}
	var count int64
	var users []models.User
	res := data_source.Db.Select("id,nickname,city,logo,shengri,name,xingzuo,height,weight,career,company,income,education,province,school,nationality,huji,children,marriage,belief,smoke,wine,biaoqian,title,family,love,images").Where("id in (?)", useridarr)

	res.Model(&models.User{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("updatetime desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := UserlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
func Collectuser(ctx *gin.Context) {
	request := UpdatezlBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	uid := request.Uid
	var collect models.Collectuser
	data_source.Db.Where("hongniangid = ?", hongniangid).Where("uid = ?", uid).First(&collect)
	if collect.ID == 0 {
		collectadd := models.Collectuser{
			Hongniangid: hongniangid,
			Uid:         uid,
		}
		data_source.Db.Create(&collectadd)
	} else {
		data_source.Db.Where("id = ?", collect.ID).Delete(&models.Collectuser{})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
